#!/usr/bin/python 
#-*- Coding: UTF-8 -*-

import socket
import struct
import sys
import time
import SocketServer
from  threading import *
import binascii
from array import *
from datetime import datetime
#from event import event

name = "Test_0001"
rendez_addr = ("0.0.0.0",5542)
local_ip = "0.0.0.0"
local_port = 5544
local_addr = ("192.168.2.24",local_port)
socket.setdefaulttimeout(3)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
print "Bind:",local_addr
sock.bind(local_addr)

print "Publish:"
sock.sendto("PUBLISH %s %s %s"%( name, local_ip, local_port), rendez_addr)
ack = sock.recv(1024)
print ack
sock.close()


sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
sock.bind(local_addr)

while 1:
    try:
        data,addr = sock.recvfrom(1024)
    except socket.timeout:
        continue
        
    print data, addr
    if data.startswith("ALIVE"):
        sock.sendto("ALIVE",addr)

sock.close()
